package com.huluobo.animdemo;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Created by LC on 2024/9/18.
 */
public class MyView extends View {
    //    //在Java中创建使用的,没有属性集合,所以需要自己给一个宽高的属性
//    public MyView(Context context) {
//        super(context);
//    }
    //画笔,绘制图形必须要有一个画笔
    private Paint paint;
    private Paint paintText;

    //在xml中使用的,他有属性集合(AttributeSet),属性集合自带宽高
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
        start(5);
    }

    private void initPaint() {
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);

        paintText = new Paint();
        paintText.setColor(Color.BLACK);
        paintText.setTextSize(100);
    }

    //绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        canvas.drawText("123", 100, 100, paint);
        RectF rectF = new RectF(100, 100, 600, 600);
//        canvas.drawRect(rectF,paint);
        canvas.drawArc(rectF, 270, progress, false, paint);
        canvas.drawText(timeString, 320, 370, paintText);
    }

    private int progress = 0;
    private String timeString;

    private void start(int time) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360);
        valueAnimator.addUpdateListener(animation -> {
            progress = (int) animation.getAnimatedValue();
            invalidate();//view刷新自身的方法
        });
        valueAnimator.setDuration(time * 1000L);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();

        ValueAnimator valueAnimatorText = ValueAnimator.ofInt(time, 0);
        valueAnimatorText.addUpdateListener(animation -> {
            timeString = String.valueOf(valueAnimatorText.getAnimatedValue());
            invalidate();
        });
        valueAnimatorText.setDuration(time * 1000L);
        valueAnimatorText.setInterpolator(new LinearInterpolator());
        valueAnimatorText.start();
    }

//    //摆放
//    @Override
//    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
//        super.onLayout(changed, left, top, right, bottom);
//    }
//
//    //测量
//    @Override
//    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//    }


    //    //在xml中使用的,多一个属性,风格的属性
//    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
//        super(context, attrs, defStyleAttr);
//    }
//
//    //与上面的类似,多了一个风格res文件
//    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
//    }
}
